package cluster

import (
	. "github.com/Monibuca/engine/v2"
	. "github.com/logrusorgru/aurora"
	"log"
)


func init() {
	InstallPlugin(&PluginConfig{
		Name:   "Api",
		Type:   PLUGIN_HOOK,
		Config: nil,
		Run:    run,
	})
}

func run() {
	Print(Green("server Api started"))
	Print("Ⓜ start Api ")
	log.Println("测试：Api")
	OnPublishHooks.AddHook(onPublish)
	OnSubscribeHooks.AddHook(onSubscribe)
}


func onSubscribe(s *Subscriber) {
	//if s.Publisher == nil {
	//	go PullUpStream(s.StreamPath)
	//}
	log.Println("测试：onSubscribe----->", s.StreamPath)
}
func onPublish(s *Stream) {
	log.Println("测试：onPublish------>", s.StreamPath)
	//if masterConn != nil {
	//	w := bufio.NewWriter(masterConn)
	//	w.WriteByte(MSG_PUBLISH)
	//	w.WriteString(s.StreamPath)
	//	w.WriteByte(0)
	//	w.Flush()
	//	stream := Subscriber{
	//		OnData: func(p *avformat.SendPacket) error {
	//			head := pool.GetSlice(9)
	//			head[0] = p.Type - 7
	//			binary.BigEndian.PutUint32(head[1:5], p.Timestamp)
	//			binary.BigEndian.PutUint32(head[5:9], uint32(len(p.Payload)))
	//			if _, err := w.Write(head); err != nil {
	//				return err
	//			}
	//			pool.RecycleSlice(head)
	//			if _, err := w.Write(p.Payload); err != nil {
	//				return err
	//			}
	//			return nil
	//		}, SubscriberInfo: SubscriberInfo{
	//			ID:   config.OriginServer,
	//			Type: "Bare",
	//		},
	//	}
	//	go stream.Subscribe(s.StreamPath)
	//}
}
